package com.zoulj.web;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.zoulj.pojo.R;
import com.zoulj.pojo.entity.UserInfoDO;
import com.zoulj.service.UserInfoService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import java.util.Arrays;

/**
* @Description: 用户表
* @Date:   2021-06-27
* @author: leonard
*/
@Slf4j
@Api(tags="用户表")
@RestController
@RequestMapping("/com.zoulj/userInfoDO")
public class UserInfoController {
   @Autowired
   private UserInfoService userInfoDOService;

   /**
    * 分页列表查询
    *
    * @param userInfoDO
    * @param pageNo
    * @param pageSize
    * @param req
    * @return
    */
   @ApiOperation(value="用户表-分页列表查询", notes="用户表-分页列表查询")
   @GetMapping(value = "/list")
   public R queryPageList(UserInfoDO userInfoDO,
                          @RequestParam(name="pageNo", defaultValue="1") Integer pageNo,
                          @RequestParam(name="pageSize", defaultValue="10") Integer pageSize,
                          HttpServletRequest req) {
       //QueryWrapper<UserInfoDO> queryWrapper = QueryGenerator.initQueryWrapper(UserInfoDO, req.getParameterMap());
       QueryWrapper<UserInfoDO> queryWrapper = new QueryWrapper();
       Page<UserInfoDO> page = new Page<UserInfoDO>(pageNo, pageSize);
       IPage<UserInfoDO> pageList = userInfoDOService.page(page, queryWrapper);
       return new R(pageList);
   }

   /**
    * 添加
    *
    * @param userInfoDO
    * @return
    */
   @ApiOperation(value="用户表-添加", notes="用户表-添加")
   @PostMapping(value = "/add")
   public R add(@RequestBody UserInfoDO userInfoDO) {
       userInfoDOService.save(userInfoDO);
       return new R("添加成功！");
   }

   /**
    * 编辑
    *
    * @param userInfoDO
    * @return
    */
   @ApiOperation(value="用户表-编辑", notes="用户表-编辑")
   @PutMapping(value = "/edit")
   public R edit(@RequestBody UserInfoDO userInfoDO) {
       userInfoDOService.updateById(userInfoDO);
       return new R("编辑成功!");
   }

   /**
    * 通过id删除
    *
    * @param id
    * @return
    */
   @ApiOperation(value="用户表-通过id删除", notes="用户表-通过id删除")
   @DeleteMapping(value = "/delete")
   public R delete(@RequestParam(name="id",required=true) String id) {
       userInfoDOService.removeById(id);
       return new R("删除成功!");
   }

   /**
    * 批量删除
    *
    * @param ids
    * @return
    */
   @ApiOperation(value="用户表-批量删除", notes="用户表-批量删除")
   @DeleteMapping(value = "/deleteBatch")
   public R deleteBatch(@RequestParam(name="ids",required=true) String ids) {
       this.userInfoDOService.removeByIds(Arrays.asList(ids.split(",")));
       return new R("批量删除成功！");
   }

   /**
    * 通过id查询
    *
    * @param id
    * @return
    */
   @ApiOperation(value="用户表-通过id查询", notes="用户表-通过id查询")
   @GetMapping(value = "/queryById")
   public R queryById(@RequestParam(name="id",required=true) String id) {
       UserInfoDO userInfoDO = userInfoDOService.getById(id);
       return new R(userInfoDO);
   }



}
